package com.sduept.nwld.dataserver.manager.etl;

import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.manager.cim.BxCimManager;
import com.sduept.bigdata.fault.common.ETLDAO;
import com.sduept.nwld.dataserver.model.edata.cim.bx.BxSubstation;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 录波器设备数据抽取
 * @author Xiaopy
 *
 */
@Service
@Slf4j
public class BxFaultRecordETLManager {

	@Autowired
	private SuperDAO dao;
	
	@Autowired
	private ETLDAO eDao;
	@Autowired
	private BxCimManager bxManager;
	
	public  void receiveAndSaveAllData() {
		List<?> list = getAllSourceData();
		receiveAndSave(list);
	}
	public  void receiveAndSavePartData(String date) {
		List<?> list = getAllSourceData(date);
		receiveAndSave(list);
	}
	
	
	public List<?> getAllSourceData(){
		String sql="select mRID,mProtectName,devManufacturer,devType,StationID,mStationID from t_bx_fault_record_equipment";
		List <?>result = eDao.query(sql);
		return result;
	}
	public List<?> getAllSourceData(String date) {
		String sql = "select mRID,mProtectName,devManufacturer,devType,StationID,mStationID from t_bx_fault_record_equipment"
				+ " where update_time=";
		List<?> result = eDao.query(sql + "'" + date + "'");
		return result;
	}
	public  void receiveAndSave(List<?> list) {
		ETLDataSave equipmentSave = new ETLDataSave(list);
		equipmentSave.saveToBean();
	}
	public class ETLDataSave{
		private List<?> list;
		private Map<String,BxSubstation> bxStationMap = new HashMap<String,BxSubstation>();
		private Map<String,BxSubstation> bxStationNameMap = new HashMap<String,BxSubstation>();
		private List<String> ids=new ArrayList<String>();
 
		public ETLDataSave(List<?> list) {
			super();
			this.list = list;
			String sql="select distinct id from t_cim_waverecord_equipment ";
			List<?> nativeQuery = dao.nativeQuery(sql);
			ids=(List<String>)nativeQuery;
            List<BxSubstation> allBxStations = bxManager.listAllBxSubstations();
			for(BxSubstation substation:allBxStations){
				if(!StringUtils.isEmpty(substation.getStationid())){
					bxStationMap.put(substation.getStationid(), substation);
					bxStationNameMap.put(substation.getMname(), substation);
				}
			}
		}
		public void saveToBean(){
			String sql="insert into t_cim_waverecord_equipment(id,name,devManufacturer,devType,privateStationId,sub_id,"
					+ "sub_name) values(?,?,?,?,?,?,?)";
			 List<Object[]> datas = new ArrayList<Object[]>();
			 
			for(Object obj : list){
				Object[] objs = (Object[]) obj;
				Object[] saveObj=new Object[7] ;
				for(int i=0;i<objs.length;i++){
					if(objs[i]!=null&&"null".equals(objs[i].toString())){
						objs[i]=null;
					}
					saveObj[i]=objs[i];
				}
				if(ids!=null&&ids.contains(saveObj[0])){
					continue;
				}
				if(objs[4]!=null){
					BxSubstation substation=bxStationMap.get(objs[4].toString());
					if(substation!=null){
						saveObj[5]=substation.getCimId();
						saveObj[6]=substation.getCimName();
					}
				}else{
					if(objs[5]!=null){
						BxSubstation substation=bxStationNameMap.get(objs[5].toString());
						if(substation!=null){
							saveObj[4]=substation.getStationid();
							saveObj[5]=substation.getCimId();
							saveObj[6]=substation.getCimName();
						}
					}
				}
				datas.add(saveObj);
			}
			try {
				dao.persistentBatch(sql, datas);
				log.info("更新故障录波器装置："+datas.size());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
